package com.example.netty.io.client

import androidx.annotation.IntRange
import com.example.netty.io.core.AbsConnector
import com.example.netty.io.core.IMessageListener
import com.example.netty.io.core.NettyConfigure
import java.net.InetSocketAddress

/**
 * File Name :NettyClient
 * Author : luqihua
 * Date : 2024/4/12
 * Desc :
 **/

class NettyClient(
    ip: String,
    @IntRange(from = 1000, to = 65535)
    port: Int,
    messageListener: IMessageListener
) : AbsConnector() {
    companion object {
        private const val TAG = "NettyClient"
    }

    private val address = InetSocketAddress(ip, port)
    private val task = NettyClientTask(address, messageListener)

    override fun start() {
        NettyConfigure.executor.execute(task)
    }

    override fun stop() {
        task.release()
    }

    fun send(message: String) {
        task.sendData(message)
    }
}